<?php

namespace App\Filament\Resources;

use App\Enums\BorrowStatus;
use App\Enums\OrderStatus;
use App\Filament\Resources\OrderResource\Pages;
use App\Filament\Resources\OrderResource\RelationManagers;
use App\Models\BorrowRecord;
use App\Models\Order;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\Action;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class OrderResource extends Resource
{
    protected static ?string $model = Order::class;

    protected static ?string $navigationIcon = 'heroicon-o-chart-bar';
    protected static ?string $navigationLabel = '订单列表';

    protected static ?string $navigationGroup = '图书管理';
    protected static ?string $modelLabel = '订单';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('book_id')
                    ->label('图书名称')
                    ->relationship('book', 'title')
                    ->required(),
                Forms\Components\Select::make('user_id')
                    ->label('用户')
                    ->relationship('user', 'name')
                    ->required(),
                Forms\Components\Select::make('category_id')
                    ->label('分类')
                    ->relationship('category', 'name')
                    ->required(),
                Forms\Components\TextInput::make('sn')
                    ->label('订单号')
                    ->required()
                    ->maxLength(255),
                Forms\Components\Toggle::make('num')
                    ->label('数量')
                    ->required(),
                Forms\Components\Toggle::make('status')
                    ->label('状态')
                    ->required(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('book.title')
                    ->label('图书名称')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('user.name')
                    ->label('用户')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('category.name')
                    ->label('分类')
                    ->numeric()
                    ->sortable(),
                Tables\Columns\TextColumn::make('sn')
                    ->label('订单号')
                    ->searchable(),
                Tables\Columns\TextColumn::make('num')
                    ->label('数量')
                    ->numeric(),
                Tables\Columns\TextColumn::make('status')
                    ->label('状态'),
                Tables\Columns\TextColumn::make('created_at')
                    ->label('下单时间')
                    ->sortable(),
                Tables\Columns\TextColumn::make('updated_at')
                    ->label('更新时间')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->actions([
                Action::make('完成订单')
                    ->visible(fn (Order $record) => $record->status != OrderStatus::HAS_COMPLETE)
                    ->label('完成')
                    ->icon('heroicon-o-document-check')
                    ->requiresConfirmation()
                    ->modalHeading('完成')
                    ->modalDescription('确认完成订单吗?')
                    ->modalSubmitActionLabel('确认')
                    ->modalCancelActionLabel('取消')
                    ->action(function (Order $record){
                        $record->update([
                            'status' => OrderStatus::HAS_COMPLETE,
                        ]);
                    }),
                Action::make('退款')
                    ->visible(fn (Order $record) => $record->status === OrderStatus::REFUND_ING)
                    ->label('退款')
                    ->color('danger')
                    ->icon('heroicon-o-lock-open')
                    ->requiresConfirmation()
                    ->modalHeading('退款')
                    ->modalDescription('确认已经退款吗?')
                    ->modalSubmitActionLabel('确认')
                    ->modalCancelActionLabel('取消')
                    ->action(function (Order $record){
                        $record->update([
                            'status' => OrderStatus::REFUND_END,
                        ]);
                    }),
               // Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListOrders::route('/'),
            'create' => Pages\CreateOrder::route('/create'),
            'edit' => Pages\EditOrder::route('/{record}/edit'),
        ];
    }
}
